package com.slz3.business.module.systemManagement.sysRoleMenu.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.systemManagement.sysPermissions.domain.SysPermissions;
import com.slz3.business.module.systemManagement.sysRoleMenu.domain.SysRoleMenu;
import com.slz3.business.module.systemManagement.sysRoleMenu.domain.SysRoleMenuFactory;
import com.slz3.business.module.systemManagement.sysRoleMenu.steward.SysRoleMenuSpecification;
import com.slz3.business.module.systemManagement.sysRolePermissions.domain.SysRolePermissions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {
    @Override
    public boolean save(SysRoleMenu sysRoleMenu) {
        return Services.sysRoleMenuTransactional.save(sysRoleMenu);
    }
    @Override
    public Page<SysRoleMenu> page(MultiValueMap<String, String> params) {
        Page<SysRoleMenu> sysRoleMenuPage = Repositorys.sysRoleMenu.findAll(
                SysRoleMenuSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return sysRoleMenuPage;
    }


    @Override
    public SysRoleMenu modify(SysRoleMenu sysRoleMenuNew) {
        return Repositorys.sysRoleMenu.saveAndFlush(SysRoleMenuFactory.initBaseFields(sysRoleMenuNew));
    }

    @Override
    public Optional<SysRoleMenu> single(UUID uuid) {
        return Repositorys.sysRoleMenu.findById(uuid);
    }

    @Override
    public List<SysRoleMenu> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.sysRoleMenu.findAllById(uuids);
    }

    @Override
    public List<SysRoleMenu> delete(List<SysRoleMenu> sysRoleMenuList) {
        sysRoleMenuList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            SysRoleMenuFactory.initBaseFields(item);
        });
        return Services.sysRoleMenuTransactional.batchModify(sysRoleMenuList);
    }

    @Override
    public SysRoleMenu info(UUID roleUuid, UUID menuUuid) {
        SysRoleMenu sysRoleMenu = new SysRoleMenu();
        sysRoleMenu.setRoleUuid(roleUuid);
        // 1. 先判断该角色是否包含该菜单
        SysRoleMenu existSysRoleMenu = Repositorys.sysRoleMenu.queryFirstByRoleUuidAndMenuUuid(roleUuid, menuUuid);
        if (existSysRoleMenu != null){
            sysRoleMenu.setSelectMenuUuids(Arrays.asList(menuUuid));
        }
        // 2. 根据菜单id获取所有的权限
        sysRoleMenu.setAllPermissionUuid(Repositorys.sysPermissions.queryByMenuUuid(menuUuid));

        // 3. 根据角色id和菜单id获取已经代理的权限
        List<SysRolePermissions> sysRolePermissions = Repositorys.sysRolePermissions.queryByRoleUuidAndMenuUuid(roleUuid, menuUuid);
        sysRoleMenu.setSelectPermissionUuid(
            Optional.ofNullable(sysRolePermissions)
                    .orElse(new ArrayList<>())
                    .stream()
                    .map(SysRolePermissions::getPermissionUuid)
                    .distinct()
                    .collect(Collectors.toList())
        );
        return sysRoleMenu;
    }
}
